/*
 * 	Flan.Zeng 2011-2016	http://git.oschina.net/signup?inviter=flan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flan.stock.view;

import com.flan.stock.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;

/**
 * 自定义带动画效果的，股票详情页面中 五档与明细的空间
 * @author flan
 * @date   2015年11月3日
 */
public class StockWdMxTabView extends RelativeLayout implements OnClickListener{
	
	//private Context context;
	private ImageView img_cursor;
	private TextView tv_tag1,tv_tag2;
	
	private int whiteColor = getResources().getColor(R.color.menu_gray);
	private int redColor = getResources().getColor(R.color.menu_red);
	
	private int currentTab = 0;
	
	private int imgWidth;
	
	public StockWdMxTabView(Context context) {
		super(context,null);
	}

	public StockWdMxTabView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		//this.context = context;
		
		TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.animationTabWidget);
		currentTab = typedArray.getInt(R.styleable.animationTabWidget_current_index, 0);
		typedArray.recycle();
		
		inflate(context, R.layout.layout_wudangmingxi_tabview, this);
		
		img_cursor = (ImageView) this.findViewById(R.id.tag_img_cursor);
		
		tv_tag1 = (TextView) this.findViewById(R.id.tv_tag1);
		tv_tag2 = (TextView) this.findViewById(R.id.tv_tag2);
		
		tv_tag1.setOnClickListener(this);
		tv_tag2.setOnClickListener(this);
		
		//获取视图的尺寸
		ViewTreeObserver vto = this.getViewTreeObserver();
		vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
			
			@Override
			public void onGlobalLayout() {
				//监听器注册后每次视图尺寸发生变化时都会调用，如果只想获取一次视图的尺寸，则需要在获取到视图尺寸后注销掉该监听器
				StockWdMxTabView.this.getViewTreeObserver().removeGlobalOnLayoutListener(this);
				initDefaultIndex(StockWdMxTabView.this.getWidth(),StockWdMxTabView.this.getHeight());
			}
		});
	}
	
	/**
	 * 初始化 默认 指示位置
	 * @param index 从1开始
	 */
	private void initDefaultIndex(int screenW,int screenH){
		
		imgWidth = screenW/2;
		
		LayoutParams para = (LayoutParams) img_cursor.getLayoutParams();
		para.width = imgWidth;
		img_cursor.setLayoutParams(para);
		
		setCurrentTab(currentTab);
	}

	@Override
	public void onClick(View v) {
		
		switch (v.getId()) {
			case R.id.tv_tag1:
				currentTab = 0;
				break;
			case R.id.tv_tag2:
				currentTab = 1;
				break;
		}
		
		setCurrentTab(currentTab);
	}
	
	/**
	 * 设置当前指示器 位置
	 * @param position
	 */
	public void setCurrentTab(int position){
		
		tv_tag1.setTextSize(12);
		tv_tag2.setTextSize(12);
		
		tv_tag1.setTextColor(whiteColor);
		tv_tag2.setTextColor(whiteColor);
		
		switch (position) {
			case 0:
				tv_tag1.setTextColor(redColor);
				tv_tag1.setTextSize(16);
				break;
			case 1:
				tv_tag2.setTextColor(redColor);
				tv_tag2.setTextSize(16);
				break;
		}
		
		Animation animation = null;
		animation = new TranslateAnimation(imgWidth*currentTab, imgWidth*position, 0, 0);
		animation.setFillAfter(true);//True:图片停在动画结束位置
		animation.setDuration(300);	//设置动画完成时间
		img_cursor.startAnimation(animation);
		
		currentTab = position;
	}
	
}





















